package com.castile.secondkill.service.impl;

import com.castile.secondkill.dao.UserDOMapper;
import com.castile.secondkill.dao.UserPasswordDOMapper;
import com.castile.secondkill.dataobject.UserDO;
import com.castile.secondkill.dataobject.UserPasswordDO;
import com.castile.secondkill.error.EmBusinessError;
import com.castile.secondkill.exception.BusinessException;
import com.castile.secondkill.service.UserService;
import com.castile.secondkill.service.model.UserModel;
import com.castile.secondkill.validator.ValidationResult;
import com.castile.secondkill.validator.ValidatorImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * UserService实现
 *
 * @author Hongliang Zhu
 * @create 2023-03-04 23:36
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDOMapper userDOMapper;

    @Autowired
    private UserPasswordDOMapper userPasswordDOMapper;

    @Autowired
    private ValidatorImpl validator;

    @Override
    public UserModel getUserById(Integer id) {
        UserDO userDO = userDOMapper.selectByPrimaryKey(id);
        if (userDO == null) {
            return null;
        }
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());

        // 对UserDo进行转换
        return userDoConvertUserModel(userDO, userPasswordDO);
    }

    private UserModel userDoConvertUserModel(UserDO userDO, UserPasswordDO userPasswordDO) {
        if (userDO == null) {
            return null;
        }
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDO, userModel);
        if (userPasswordDO != null) {
            userModel.setEncriptPassword(userPasswordDO.getEncriptPassword());
        }

        return userModel;
    }

    @Override
    public void register(UserModel userModel) throws BusinessException {
        // UserModel 转换成UserDO
        if (userModel == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATIONERROR);
        }
        // 进行校验
        ValidationResult validationResult = validator.validate(userModel);

        if (validationResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATIONERROR, validationResult.getErrorMsg());
        }

        UserDO userDo = convertUserDoFrom(userModel);
        try {
            userDOMapper.insertSelective(userDo);
        } catch (DuplicateKeyException ex) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATIONERROR, "该手机号已注册");
        }

        // 写入密码
        userModel.setId(userDo.getId());
        // 密码
        UserPasswordDO userPasswordDO = convertPasswordFromModel(userModel);
        userPasswordDOMapper.insertSelective(userPasswordDO);
    }

    private UserPasswordDO convertPasswordFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserPasswordDO userPasswordDO = new UserPasswordDO();
        userPasswordDO.setEncriptPassword(userModel.getEncriptPassword());
        userPasswordDO.setUserId(userModel.getId());
        return userPasswordDO;
    }

    private UserDO convertUserDoFrom(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userModel, userDO);
        return userDO;
    }

    @Override
    public UserModel validateLogin(String telephone, String encriptPass) throws BusinessException {
        // 获取用户信息
        UserDO userDO = userDOMapper.selectByTelephone(telephone);
        if (userDO == null) {
            // 查不到用户信息
            throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
        }
        // 拿到密码
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
        UserModel userModel = userDoConvertUserModel(userDO, userPasswordDO);
        // 校验密码
        if (!StringUtils.equals(encriptPass, userModel.getEncriptPassword())) {
            throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
        }
        return userModel;
    }


}
